package com.async.designpatterns.behavior.state;

import com.async.designpatterns.behavior.state.state.Blocked;
import com.async.designpatterns.behavior.state.state.New;
import com.async.designpatterns.behavior.state.state.Running;
import com.async.designpatterns.behavior.state.state.ThreadState;
import com.async.designpatterns.behavior.state.state.Runnable;
/**
 * 定义线程上下文类，用于获取线程当前状态以及状态切换操作
 */
public class ThreadContext {

    private ThreadState threadState;

    /**
     * 初始化
     */
    public ThreadContext(){
        threadState = new New();
    }

    /**
     * 创建成功
     */
    public void start()
    {
        ((New) threadState).start(this);
    }

    /**
     * 就绪状态
     */
    public void getCPU()
    {
        ((Runnable) threadState).getCPU(this);
    }

    /**
     * 挂起状态
     */
    public void suspend()
    {
        ((Running) threadState).suspend(this);
    }

    /**
     * 停止状态
     */
    public void stop()
    {
        ((Running) threadState).stop(this);
    }

    /**
     * 唤醒状态
     */
    public void resume()
    {
        ((Blocked) threadState).resume(this);
    }

    public void setState(ThreadState state){
        this.threadState = state;
    }

    public void getState(){
        System.out.println("the thread state is :"+threadState.getStateName());
    }
}
